﻿using UnityEngine;
using System.Collections;

public class AttackBehaviour : BehaviourNode {

    public string animationName;
    public float range;
    public float damage;
    public float timeOfImpact;
    public float timeAfter;

    public override IEnumerator Activate()
    {
        running = true;
        while (this.Target() && this.Alive() && this.Target().Alive() && Vector3.Distance(this.Target().transform.position, transform.position) <= range)
        {
            this.CanMove(false);
            var target = this.Target();
            this.PlayAnim(animationName);
            yield return new WaitForSeconds(timeOfImpact);
            if (target && this.Alive())
                target.Attack(damage);
            yield return new WaitForSeconds(timeAfter);
            this.CanMove(true);
        }

        running = false;
    }
}
